package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Funcionario_Banco;
import br.com.evl.tecinfo.dao.RegistroHoraExtra_Banco;
import br.com.evl.tecinfo.modelo.Funcionario;
import br.com.evl.tecinfo.modelo.RegistroHoraExtra;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Tecinfo
 */
public class TelaRegistroHoraExtra extends javax.swing.JFrame {

    public RegistroHoraExtra novoregistroHora;
    private RegistroHoraExtra_Banco horaBD;
    private Funcionario_Banco funcionarioBD;

    public TelaSelecionarFuncionario telaFuncionario;
    /**
     * Creates new form TelaRegistroHoraExtra
     */

    int editar = 0;

    public TelaRegistroHoraExtra() {
        initComponents();
        
        atualizarTabela();

        telaFuncionario = new TelaSelecionarFuncionario();
        novoregistroHora = new RegistroHoraExtra();     
        horaBD= new RegistroHoraExtra_Banco();
        
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jCB_mes = new javax.swing.JComboBox();
        jCB_ano = new javax.swing.JComboBox();
        JT_BH = new javax.swing.JTextField();
        JT_outros = new javax.swing.JTextField();
        jBcadastrar_tc1 = new javax.swing.JButton();
        jB_editar1 = new javax.swing.JButton();
        jB_remover = new javax.swing.JButton();
        JB_calcular = new javax.swing.JButton();
        JB_voltar = new javax.swing.JButton();
        jcbFuncionarios = new javax.swing.JComboBox();
        jlblFuncionario = new javax.swing.JLabel();
        jbtnFuncionario = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jtfQuantHora = new javax.swing.JTextField();
        jtfAtividades = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        JL_horaExtra = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        JT_Table = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(600, 600));
        setMinimumSize(new java.awt.Dimension(600, 600));
        setResizable(false);
        getContentPane().setLayout(null);

        jCB_mes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembo" }));
        getContentPane().add(jCB_mes);
        jCB_mes.setBounds(140, 340, 80, 24);

        jCB_ano.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2013", "2014", "2015", "2016" }));
        getContentPane().add(jCB_ano);
        jCB_ano.setBounds(40, 340, 70, 24);

        JT_BH.setText("23");
        JT_BH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_BHActionPerformed(evt);
            }
        });
        getContentPane().add(JT_BH);
        JT_BH.setBounds(140, 260, 420, 30);

        JT_outros.setText("Outros");
        getContentPane().add(JT_outros);
        JT_outros.setBounds(140, 300, 420, 30);

        jBcadastrar_tc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBcadastrar_tc1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_add1v2.png"))); // NOI18N
        jBcadastrar_tc1.setText("Cadastrar");
        jBcadastrar_tc1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcadastrar_tc1ActionPerformed(evt);
            }
        });
        getContentPane().add(jBcadastrar_tc1);
        jBcadastrar_tc1.setBounds(40, 550, 120, 40);

        jB_editar1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_editar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icon_editar1v2.png"))); // NOI18N
        jB_editar1.setText("Editar");
        jB_editar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_editar1ActionPerformed(evt);
            }
        });
        getContentPane().add(jB_editar1);
        jB_editar1.setBounds(170, 550, 120, 40);

        jB_remover.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jB_remover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/icone_excluirv2.png"))); // NOI18N
        jB_remover.setText("Remover");
        jB_remover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_removerActionPerformed(evt);
            }
        });
        getContentPane().add(jB_remover);
        jB_remover.setBounds(310, 550, 120, 40);

        JB_calcular.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        JB_calcular.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/accessories_calculatorv2.png"))); // NOI18N
        JB_calcular.setText("Calcular");
        JB_calcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_calcularActionPerformed(evt);
            }
        });
        getContentPane().add(JB_calcular);
        JB_calcular.setBounds(440, 550, 120, 40);

        JB_voltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/go-back.png"))); // NOI18N
        JB_voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_voltarActionPerformed(evt);
            }
        });
        getContentPane().add(JB_voltar);
        JB_voltar.setBounds(540, 10, 50, 40);

        getContentPane().add(jcbFuncionarios);
        jcbFuncionarios.setBounds(160, 90, 320, 24);

        jlblFuncionario.setText("Selecione um funcionário...");
        jlblFuncionario.setEnabled(false);
        getContentPane().add(jlblFuncionario);
        jlblFuncionario.setBounds(190, 180, 360, 30);

        jbtnFuncionario.setText("Funcionário");
        jbtnFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnFuncionarioActionPerformed(evt);
            }
        });
        getContentPane().add(jbtnFuncionario);
        jbtnFuncionario.setBounds(60, 180, 120, 25);

        jLabel9.setText("Func");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(100, 100, 51, 15);

        jtfQuantHora.setText("12");
        getContentPane().add(jtfQuantHora);
        jtfQuantHora.setBounds(140, 220, 420, 30);

        jtfAtividades.setText("12");
        getContentPane().add(jtfAtividades);
        jtfAtividades.setBounds(150, 140, 420, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Quant. Hora:");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(40, 230, 90, 17);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/logomarcav2.png"))); // NOI18N
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 0, 140, 110);

        JL_horaExtra.setFont(new java.awt.Font("Trebuchet MS", 1, 30)); // NOI18N
        JL_horaExtra.setText("Hora Extra");
        getContentPane().add(JL_horaExtra);
        JL_horaExtra.setBounds(210, 40, 180, 50);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("BH:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(40, 270, 50, 17);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Outros:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(40, 310, 70, 17);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Atividades");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(40, 150, 90, 17);

        jLabel5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(jLabel5);
        jLabel5.setBounds(20, 120, 560, 250);

        JT_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mês", "Ano", "Quant Hora", "BH", "Atividades", "Funcionários", "Outros", "Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(JT_Table);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(40, 380, 520, 160);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/back_azul.jpg"))); // NOI18N
        jLabel7.setMaximumSize(new java.awt.Dimension(600, 600));
        getContentPane().add(jLabel7);
        jLabel7.setBounds(0, 0, 600, 600);

        setSize(new java.awt.Dimension(616, 638));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void JT_BHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_BHActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JT_BHActionPerformed

    private void jBcadastrar_tc1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcadastrar_tc1ActionPerformed

        novoregistroHora.setMes(Integer.parseInt((String) jCB_mes.getSelectedItem()));
        novoregistroHora.setAno(Integer.parseInt((String) jCB_ano.getSelectedItem()));
        novoregistroHora.setQuantDeHora(Double.valueOf(jtfQuantHora.getText()));
        novoregistroHora.setBHMes(Double.valueOf(JT_BH.getText()));
        novoregistroHora.setAtividade(jtfAtividades.getText());
        novoregistroHora.setOutros(JT_outros.getText());
        novoregistroHora.setTotal(telaFuncionario.funcionario.getValorHora() * novoregistroHora.getQuantDeHora());
        
        horaBD.inserir(novoregistroHora);
       
        atualizarTabela();
        

//        if (cadastrar_editar == -1) {
//            DefaultTableModel dtm = (DefaultTableModel) JT_Table.getModel();
//            Object[] opcoes = { JT_hora_extra.getText(),
//                JT_BH.getText(), JT_outros.getText()};
//            dtm.addRow(opcoes);
//            JT_hora_extra.setText("");
//            JT_BH.setText("");
//            JT_outros.setText("");
//
//        } else {
//            int atualizar = JT_Table.getSelectedRow();
//            JT_Table.setValueAt(JT_hora_extra.getText(), atualizar, 1);
//            JT_Table.setValueAt(JT_BH.getText(), atualizar, 2);
//            JT_Table.setValueAt(JT_outros.getText(), atualizar, 3);
//            
//            JOptionPane.showMessageDialog(null, "Dados atualizados com sucesso!");
//            cadastrar_editar = -1;
//            JT_hora_extra.setText("");
//            JT_BH.setText("");
//            JT_outros.setText("");
//        }
    }//GEN-LAST:event_jBcadastrar_tc1ActionPerformed

    private void jB_editar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_editar1ActionPerformed
       
        if (editar == 0) {

            jCB_mes.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 0));
            jCB_ano.setSelectedItem((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 1));
            jtfQuantHora.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 2));
            JT_BH.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 3));
            jtfAtividades.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 4));
            jlblFuncionario.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 5));
            JT_outros.setText((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 6));
  
            jB_editar1.setText("Concluir");
            jB_remover.setEnabled(false);
            jBcadastrar_tc1.setEnabled(false);
            JT_Table.setEnabled(false);
            editar = 1;

        } else {

            jB_removerActionPerformed(evt);
            jBcadastrar_tc1ActionPerformed(evt);

            jB_editar1.setText("Editar");
            jB_remover.setEnabled(true);
            jBcadastrar_tc1.setEnabled(true);
            JT_Table.setEnabled(true);
            editar = 0;

        }

    }//GEN-LAST:event_jB_editar1ActionPerformed

    private void jB_removerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_removerActionPerformed
             
        RegistroHoraExtra removeHora = new RegistroHoraExtra();
        
        Funcionario func = new Funcionario();
        
        func.setCpf((String) JT_Table.getValueAt(JT_Table.getSelectedRow(), 5));
        
        removeHora.setFuncionario(func);        

        horaBD.remover(removeHora);
        
        atualizarTabela();
        
        /*DefaultTableModel dtm = (DefaultTableModel) JT_Table.getModel();
         Object[] opcoes = { JT_hora_extra.getText(),
         JT_BH.getText(), JT_outros.getText()   };
         int row = JT_Table.getSelectedRow();
         dtm.removeRow(row);

       
         JT_hora_extra.setText("");
         JT_BH.setText("");
         JT_outros.setText("");*/
    }//GEN-LAST:event_jB_removerActionPerformed

    private void JB_calcularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_calcularActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JB_calcularActionPerformed

    private void JB_voltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_voltarActionPerformed

        this.setVisible(false);

    }//GEN-LAST:event_JB_voltarActionPerformed

    private void jbtnFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnFuncionarioActionPerformed

        if (telaFuncionario.isVisible() == false) {
            telaFuncionario = new TelaSelecionarFuncionario();
            telaFuncionario.setVisible(true);
        }

    }//GEN-LAST:event_jbtnFuncionarioActionPerformed

    private void atualizarTabela() {

         horaBD = new RegistroHoraExtra_Banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Mês", "Ano", "Quant Hora", "BH", "Atividades", "Funcionários", "Outros", "Total"});

        try {
            while(horaBD.consulta.next()) {

                Object[] opcoes = {horaBD.consulta.getString("mes"), horaBD.consulta.getString("ano"),
                    horaBD.consulta.getString("quantidadeDeHora"), horaBD.consulta.getString("bHMes"),
                    horaBD.consulta.getString("atividade"), horaBD.consulta.getString("funcionario"),
                    horaBD.consulta.getString("outros"), horaBD.consulta.getString("total")};
                dtm.addRow(opcoes);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        JT_Table.setModel(dtm);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_calcular;
    private javax.swing.JButton JB_voltar;
    private javax.swing.JLabel JL_horaExtra;
    private javax.swing.JTextField JT_BH;
    private javax.swing.JTable JT_Table;
    private javax.swing.JTextField JT_outros;
    private javax.swing.JButton jB_editar1;
    private javax.swing.JButton jB_remover;
    private javax.swing.JButton jBcadastrar_tc1;
    private javax.swing.JComboBox jCB_ano;
    private javax.swing.JComboBox jCB_mes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnFuncionario;
    private javax.swing.JComboBox jcbFuncionarios;
    public javax.swing.JLabel jlblFuncionario;
    private javax.swing.JTextField jtfAtividades;
    private javax.swing.JTextField jtfQuantHora;
    // End of variables declaration//GEN-END:variables
}
